package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/3 15:50
 */

@DubboService
public class CommentsApiImpl implements CommentsApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据userId和type=2(评论)查询comment
     * @param page
     * @param pageSize
     * @param movementId
     * @param comment
     * @return
     */
    @Override
    public List<Comment> findComments(Integer page, Integer pageSize, String movementId, CommentType comment) {
        // 根据id和commentType查询
        Criteria criteria = Criteria
                .where("publishId")
                .is(new ObjectId(movementId))
                .and("commentType")
                .is(comment.getType());
        Query query = Query.query(criteria)
                .skip((page - 1) * pageSize)
                .limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    /**
     * 保存评论
     * @param comment
     * @return
     */
    @Override
    public Integer saveComments(Comment comment) {

        // 查询动态表
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if(movement != null){
            // 给评论表设置动态id
            comment.setPublishUserId(movement.getUserId());
        }
        // 将数据保存到评论表
        mongoTemplate.save(comment);

        // 更新动态表数据数据
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        Update update = new Update();
        // 判断操作的是评论，点赞，还是喜欢
        if(comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount", 1);
        }else if (comment.getCommentType() == CommentType.LOVE.getType()){
            update.inc("loveCount", 1);
        }else {
            update.inc("likeCount", 1);
        }


        FindAndModifyOptions options = new FindAndModifyOptions();
        // 获取更新后的数据
        options.returnNew(true);
        Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        Integer count = andModify.statisCount(comment.getCommentType());
        return count;
    }

    /**
     * 判断是否点赞
     * @param movementId
     * @param userId
     * @param like
     * @return
     */
    @Override
    public boolean hasComment(String movementId, Long userId, CommentType like) {

        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(like.getType());
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, Comment.class);
        return exists;
    }

    /**
     * 取消点赞
     * @param comment
     * @return
     */
    @Override
    public Integer removeComment(Comment comment) {

        // 删除Comment数据, 根据comment的id和类型=2
        Criteria criteria = Criteria
                .where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);

        // 修改动态表中的数据
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId())); // 根据动态表id

        Update update = new Update();
        // 判断是点赞|评论|喜欢
        if(comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount", -1);
        }else if(comment.getCommentType() == CommentType.LOVE.getType()){
            update.inc("loveCount", -1);
        }else {
            update.inc("likeCount", -1);
        }

        FindAndModifyOptions option = new FindAndModifyOptions();
        option.returnNew(true);
        Movement andModify = mongoTemplate.findAndModify(movementQuery, update, option, Movement.class);
        Integer count = andModify.statisCount(comment.getCommentType());
        return count;
    }
}
